# coding:utf-8
"""
description:sign up页面
"""
import os

from common import Base_Page
from appium.webdriver.common import mobileby

from common.Get_Yaml import get_yaml
from common.Log import LOG
from config.GlobalParameter import TEST_Element_YAML

testData = get_yaml(os.path.join(TEST_Element_YAML, 'SignUp.yaml'))


class SignUp_page(Base_Page.Base_page):
    By = mobileby.MobileBy()

    linkSignUp_loc = (By.ID, testData.get_elementInfo(0))

    def click_linksignUp(self):
        """点击sign up link"""
        self.find_element(*self.linkSignUp_loc).click()

    etRegisterUsername_loc = (By.ID, testData.get_elementInfo(1))
    etRegisterEmail_loc = (By.ID, testData.get_elementInfo(2))
    etRegisterPassword_loc = (By.ID, testData.get_elementInfo(3))
    etRegisterPasswordCfm_loc = (By.ID, testData.get_elementInfo(4))
    linktvAgreement1_loc = (By.ID, testData.get_elementInfo(5))
    linktvAgreement2_loc = (By.ID, testData.get_elementInfo(6))
    cbAgreement_loc = (By.ID, testData.get_elementInfo(7))
    cbPromotions_loc = (By.ID, testData.get_elementInfo(8))
    btnRegister_loc = (By.ID, testData.get_elementInfo(9))
    btnBack_loc = (By.ID, testData.get_elementInfo(10))

    def func_sign_up(self, username, email, pwd, pwdCfm, message):
        """
        注册四个输入框验证，和sign up成功的方法
        :param username:
        :param email:
        :param pwd:
        :param pwdCfm:
        :param message:
        :return:
        """
        self.input_etRegisterUsername(username)
        self.input_etRegisterEmail(email)
        self.input_etRegisterPassword(pwd)
        self.input_etRegisterPasswordcfm(pwdCfm)
        self.select_cbAgreement()
        self.click_btnRegister()
        return self.find_toast(message)

    def func_CheckBox1(self):
        """
        验证'I accept the Privacy Policy……‘弹框未选中时，断言sign up不能点击
        :return:
        """
        checked = self.get_cbAgreement().get_attribute("checked")
        enabled = self.get_btnRegister().get_attribute("enabled")
        LOG.info("'I accept the Privacy Policy……‘单选框属性checked状态：{}。Register按钮属性enabled状态：{}".format(checked, enabled))
        if checked == 'false':
            return enabled == "false"  # accept单选框未选中，则register按钮不能点击
        else:
            return enabled == "true"  # accept单选框选中，则register按钮能点击

    def func_enterSignUp(self):
        """
        测试进入sign up page,断言btnRegister按钮存在
        :return:
        """
        self.click_linksignUp()
        # 判断btnRegister按钮是否存在
        return self.assertVisible_btnRegister()

    def assertVisible_btnRegister(self):
        """
        判断btnRegister按钮是否存在
        :return:
        """
        return self.assertVisible(*self.btnRegister_loc)

    def get_btnRegister(self):
        """获取btnRegister"""
        return self.find_element(*self.btnRegister_loc)

    def input_etRegisterUsername(self, etRegisterUsername):
        """输入RegisterUsername"""
        self.send_keys(etRegisterUsername, self.find_element(*self.etRegisterUsername_loc))

    def input_etRegisterEmail(self, etRegisterEmail):
        """输入RegisterEmail"""
        self.send_keys(etRegisterEmail, self.find_element(*self.etRegisterEmail_loc))

    def input_etRegisterPassword(self, etRegisterPassword):
        """输入RegisterPassword"""
        self.send_keys(etRegisterPassword, self.find_element(*self.etRegisterPassword_loc))

    def input_etRegisterPasswordcfm(self, etRegisterPasswordCfm):
        """输入RegisterPasswordCfm"""
        self.send_keys(etRegisterPasswordCfm, self.find_element(*self.etRegisterPasswordCfm_loc))

    def click_tvAgreement1(self):
        """点击Privacy Policy link"""
        self.find_element(*self.linktvAgreement1_loc).click()

    def click_tvAgreement2(self):
        """点击 Software License & Service Agreement link"""
        self.find_element(*self.linktvAgreement2_loc).click()

    def click_btnRegister(self):
        """点击Sign Up按钮"""
        self.find_element(*self.btnRegister_loc).click()

    def click_btnBack(self):
        """点击Back按钮"""
        self.find_element(*self.btnBack_loc).click()

    def get_cbAgreement(self):
        """选中同意Agreement选项"""
        return self.find_element(*self.cbAgreement_loc)

    def select_cbAgreement(self):
        """选中同意Agreement选项"""
        cbAgreement = self.find_element(*self.cbAgreement_loc)
        if cbAgreement.get_attribute("checked") == 'false':
            cbAgreement.click()

    def select_cbPromotions(self):
        """选中 Promotions选项"""
        cbPromotions = self.find_element(*self.cbPromotions_loc)
        if cbPromotions.get_attribute("checked") == 'false':
            cbPromotions.click()
